import { createPinia } from 'pinia'
import piniaPersist from 'pinia-plugin-persist-uni'

// 自动注入所有pinia模块
const files = import.meta.glob('./**/*.ts', { eager: true }) as any
console.log(
  '%c files: ',
  'font-size:14px;color:#7ec050;',
  files,
)
const modules = {} as any
Object.keys(files).forEach((key) => {
  modules[key.replace(/(.*\/)*([^.]+).*/g, '$2')] = files[key].default
})
console.log(
  '%c modules: ',
  'font-size:14px;color:#7ec050;',
  modules,
)

export function setupPinia(app: any) {
  const pinia = createPinia()
  pinia.use(piniaPersist)

  app.use(pinia)
}

export default (name: any) => {
  return modules[name]()
}
